package com.example.mymvvmdemo.net.model;


import androidx.annotation.NonNull;


import com.example.mymvvmdemo.api.API;
import com.example.mymvvmdemo.retrofit.RetrofitManager;
import com.example.mymvvmdemo.utils.LogUtils;

import org.json.JSONObject;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * 普通Get请求统一model
 */
public class GetModel extends BaseModel {

    private IModel.GetModel getModel;

    public GetModel(IModel.GetModel getModel) {
        this.getModel = getModel;
    }

    public void startLoadGet(String url, String auth_token, HashMap<String, String> hashMap, String type) {
        // Format the auth token with Bearer prefix
        String formattedToken = "Bearer " + auth_token;

        RetrofitManager.getInstance().getRetrofit().create(API.class).loadGet(
                        url, formattedToken, hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseBody response) {

                        try {
                            String result = response.string();
                            //判断请求网络是否异常
                            if (isNetWorkError(result)) {
                                getModel.loadGetNetWorkError(result);
                                return;
                            }

                            JSONObject jsonObject = new JSONObject(result);
                            int code = jsonObject.getInt("code");
                            //检查是否账号异常（被挤或者token异常)
//                            boolean isError = CheckOutUtils.checkErrorCode(code);
//                            if (isError) {
//                                getModel.loadGetAccountError(code);
//                            } else {
                            getModel.loadGetSuccess(result, type, code);
//                            }

                        } catch (Exception e) {
                            LogUtils.e(e.getMessage());
                            getModel.loadGetFail(e.getMessage(), type);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //判断请求网络是否异常
                        if (isNetWorkError(e.getMessage())) {
                            getModel.loadGetNetWorkError(e.getMessage());
                            return;
                        }

                        getModel.loadGetFail(e.getMessage(), type);
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }

}
